package com.xx.job.service.system.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xx.job.controller.dto.PageDTO;
import com.xx.job.entity.system.SysDictData;
import com.xx.job.mapper.SysDictDataMapper;
import com.xx.job.service.system.SysDictDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liangcy
 * @since 2021-01-14
 */
@Service
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDictData> implements SysDictDataService {

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    public List<SysDictData> dictData(String dictType){
        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("dict_type",dictType);
        return sysDictDataMapper.selectByMap(columnMap);
    }

    @Override
    public int add(SysDictData sysDictData) {
        return sysDictDataMapper.insert(sysDictData);
    }

    @Override
    public int delete(Long id) {
        return sysDictDataMapper.deleteById(id);
    }

    @Override
    public int update(SysDictData sysDictData) {
        return sysDictDataMapper.updateById(sysDictData);
    }

    @Override
    public SysDictData select(Long id) {
        return sysDictDataMapper.selectById(id);
    }

    @Override
    public Map<String, Object> selectPage(PageDTO pageDTO,SysDictData sysDictData) {
        Map<String, Object> data = new HashMap<>();
        IPage<SysDictData> page = new Page<>(pageDTO.getPageIndex(),pageDTO.getPageSize());
        QueryWrapper<SysDictData> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(sysDictData.getDictType())){
            wrapper.eq("dict_type",sysDictData.getDictType());
        }
        if(StringUtils.isNotBlank(sysDictData.getStatus())){
            wrapper.eq("status",sysDictData.getStatus());
        }
        if(StringUtils.isNotBlank(sysDictData.getDictLabel())){
            wrapper.eq("dict_label",sysDictData.getDictLabel().trim());
        }
        IPage<SysDictData> sysDictDataIPage = sysDictDataMapper.selectPage(page, wrapper);
        data.put("count",sysDictDataIPage.getTotal());
        data.put("list",sysDictDataIPage.getRecords());
        data.put("pageIndex",pageDTO.getPageIndex());
        data.put("pageSize", pageDTO.getPageSize());
        return data;
    }
}
